package com.junchenrobot.mes.dao;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.junchenrobot.mes.domain.Winder;
import com.junchenrobot.mes.mapper.WinderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 卷绕机Dao
 * 包含卷绕机数据库操作
 *
 * @author SeaWave
 * @date 2023/01/05
 */
@Component
public class WinderDao {

    @Autowired
    private WinderMapper winderMapper;


    public Winder listPosition(String line, String spinning) {
        return new LambdaQueryChainWrapper<>(winderMapper).eq(Winder::getWinderLine, line)
                                                          .eq(Winder::getWinderSpinningCode, spinning)
                                                          .last("LIMIT 1")
                                                          .one();
    }

    public Integer selectOffset(Integer line, Integer spinning) {
        Winder one = new LambdaQueryChainWrapper<>(winderMapper).select(Winder::getWinderOffset)
                                                                .eq(Winder::getWinderLine, line)
                                                                .eq(Winder::getWinderSpinningCode, spinning)
                                                                .last("LIMIT 1")
                                                                .one();
        return Objects.isNull(one) ? 0 : one.getWinderOffset();
    }


    public Winder getWinder(Integer line, Integer spinning) {
        return new LambdaQueryChainWrapper<>(winderMapper).eq(Winder::getWinderLine, line)
                                                          .eq(Winder::getWinderSpinningCode, spinning)
                                                          .last("LIMIT 1")
                                                          .one();
    }

    public Winder getWinder(Long winderId) {
        return new LambdaQueryChainWrapper<>(winderMapper).eq(true, Winder::getId, winderId)
                                                          .last("LIMIT 1")
                                                          .one();
    }

    public Winder getWinder(String winderName) {
        return new LambdaQueryChainWrapper<>(winderMapper).eq(true, Winder::getWinderName, winderName)
                                                          .last("LIMIT 1")
                                                          .one();
    }


    public List<Winder> listWinder() {
        return new LambdaQueryChainWrapper<>(winderMapper).select(Winder::getId, Winder::getWinderName)
                                                          .list();
    }
}
